home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************************
- * *
- * MemAlloc.c *
- * *
- * ©1995 Douglas Grounds. All Rights Reserved. *
- * *
- * This file contains memory allocation functions which are quite handy. *
- * *
- ************************************************************************************/
-
- #include <Memory.h>
- #include <Types.h>
-
- #ifndef TRUE
- #define TRUE 1
- #endif
-
- #ifndef FALSE
- #define FALSE 0
- #endif
-
- #include "MemAlloc.h"
- #include "ErrCheck.h"
-
- /*-----------------------------------
- *
- * Global Variables
- *
- * Keeps track of how much memory
- * the application has allocated
- * vs. Resource Mgr, Toolbox, System,
- * etc.
- *
- *-----------------------------------*/
-
- long gMemoryAllocated = 0L; // Total memory allocated by application, in bytes.
- long gMemHandlesAllocated = 0L; // Total handles allocated by application, in bytes.
- long gMemPointersAllocated = 0L; // Total pointers allocated by application, in bytes.
-
- /*-----------------------------------
- *
- * Example Usage
- *
- *-----------------------------------*/
-
- #if (FALSE)
-
- void myExampleFunction (void)
- {
- Handle hndl;
-
- hndl = maNewHandle(1024, FALSE);
- if (hndl == NULL)
- return;
-
- ... use "hndl" ...
-
- hndl = maDisposeHandle(hndl); // Sets "hndl" to NULL for safety
-
- myPtr = maNewPtr(1024, TRUE);
- if (myPtr == NULL)
- return;
-
- ... use "myPtr" ...
-
- myPtr = maDisposePtr(myPtr); // Sets "myPtr" to NULL for safety
- }
-
- #endif
-
- /******************************************************
- * maNewHandle. *
- * *
- * Allocates a new relocateable block of memory. *
- * Returns NULL if an error occurs and keeps track *
- * of total memory allocated. Use MADisposeHandle *
- * to dispose of the block once you've finished. *
- ******************************************************/
-
- Handle maNewHandle (long sizeNeeded, Boolean clear)
- {
- Handle hndl;
-
- if (clear)
- hndl = NewHandleClear(sizeNeeded);
- else
- hndl = NewHandle(sizeNeeded);
-
- if (checkMemError(hndl))
- return NULL;
-
- gMemoryAllocated += sizeNeeded;
- gMemHandlesAllocated += sizeNeeded;
-
- return hndl;
- }
-
- /******************************************************
- * maDisposeHandle. *
- * *
- * Disposes of a handle. Make sure you've used *
- * MANewHandle to allocate to keep track of memory *
- * correctly. Returns NULL for convenience. *
- ******************************************************/
-
- Handle maDisposeHandle (Handle doomedHandle)
- {
- long howBig;
-
- #if __YOU_WANT_DEBUGGING
- // Null, odd address or unrecognized by Memory Manager are all
- // problems!
- if ((doomedHandle == NULL) || (doomedHandle & 0x01) ||
- (*doomedHandle == NULL) || (*doomedHandle & 0x01) || (GetHandleSize(doomedHandle) == 0L))
- {
- // Probably should handle this, buddy-boy.
- return NULL;
- }
- #endif
-
- gMemoryAllocated -= (howBig = GetHandleSize(doomedHandle));
- gMemHandlesAllocated -= howBig;
- DisposeHandle(doomedHandle);
- return NULL;
- }
-
- /******************************************************
- * maNewPtr. *
- * *
- * Allocate a new nonrelocateable block of memory. *
- * Returns NULL if an error occurs and keeps track *
- * of total memory allocated. Use MADisposePtr *
- * to dispose of the block once you've finished. *
- ******************************************************/
-
- Ptr maNewPtr (long sizeNeeded, Boolean clear)
- {
- Ptr myPtr;
-
- if (clear)
- myPtr = NewPtrClear(sizeNeeded);
- else
- myPtr = NewPtr(sizeNeeded);
-
- if (checkMemError(myPtr))
- return NULL;
-
- gMemoryAllocated += sizeNeeded;
- gMemPointersAllocated += sizeNeeded;
-
- return myPtr;
- }
-
- /******************************************************
- * maDisposePtr. *
- * *
- * Disposes of a pointer. Make sure you've used *
- * MANewPtr to allocate to keep track of memory *
- * correctly. Returns NULL for convenience. *
- ******************************************************/
-
- Ptr maDisposePtr (Ptr doomedPtr)
- {
- long howBig;
-
- #if __YOU_WANT_DEBUGGING
- // Null, odd address or unrecognized by Memory Manager are all
- // problems!
- if ((doomedPtr == NULL) || (doomedPtr & 0x01) || (GetPtrSize(doomedPtr) == 0L))
- {
- // Probably should handle this, buddy-boy.
- return NULL;
- }
- #endif
-
- gMemoryAllocated -= (howBig = GetPtrSize(doomedPtr));
- gMemPointersAllocated -= howBig;
- DisposePtr(doomedPtr);
- return NULL;
- }